<?php

namespace App\Exports;

use App\Models\InvitationRegister;
use Illuminate\Contracts\Support\Responsable;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ConvertRecordExport implements FromArray, WithMapping, WithHeadings, Responsable
{
    use Exportable;

    /** @var InvitationRegister[] */
    protected $data;
    private $fileName;

    public function __construct($activityName, array $data)
    {
        $this->data = $data; // 实例化该脚本的时候，需要传入要导出的数据
        $this->fileName = sprintf('[%s]合成记录_%s.xlsx', $activityName, now()->format('YmdHis')); // 指定导出的文件名
    }

    public function array(): array // 返回的数据
    {
        return $this->data;
    }

    public function map($value): array // 指定excel中每一列的数据字段
    {
        return [
            $value['user']['auth'] ? $value['user']['auth']['real_name'] : $value['user']['mobile'],
            $value['user']['mobile'],
            $value['convert_count'],
            $value['created_at'],
            $value['status']->description,
        ];
    }

    public function headings(): array // 指定excel的表头
    {
        return [
            '合成用户',
            '手机号',
            '数量',
            '合成时间',
            '状态',
        ];
    }
}
